package com.sh1604.duitang.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.sh1604.duitang.R;
import com.sh1604.duitang.constant.UrlString;
import com.sh1604.duitang.mybean.FindImageBean;
import com.squareup.picasso.Picasso;

import java.io.IOException;

/**
 * ***********************************************************
 * author: jie
 * time: 2016/7/20 10:04
 * name:
 * overview:Build.VERSION.SDK_INT:版本号
 * usage:
 * *************************************************************
 */
public class FindImageView extends LinearLayout{

    private ImageView find_image_iv;

    public FindImageView(Context context) {
        this(context,null);
    }


    public FindImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FindImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_find_image, this);
        find_image_iv = (ImageView) view.findViewById(R.id.view_find_image);

        com.sh1604.duitang.utils.OKHttpUtils.newInstance(getContext()).getAsyncMainData(UrlString.URL_Image, new com.sh1604.duitang.utils.OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                final FindImageBean bean = new Gson().fromJson(result,FindImageBean.class);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Picasso.with(getContext()).load(bean.getData().getObject_list().get(0).getImage_url()).into(find_image_iv);
                    }
                });
            }
        });
    }
    Handler mHandler = new Handler();
}
